package com.ruoyi.custom;



import com.ruoyi.model.LoginMember;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 自定义权限校验
 */
@Component("qtu")
public class QtuExpression {
    /**
     * 判断用户是否有相应的权限
     * @param permission 权限
     * @return
     */
    public boolean hasPermission(String permission){
        Authentication authentication= SecurityContextHolder.getContext().getAuthentication();
        LoginMember member=(LoginMember)authentication.getPrincipal();
        List<String> permissions = member.getPermissions();
        return permissions.contains(permission);
    }

    public boolean hasAnyPermission(String... permission){
        Authentication authentication= SecurityContextHolder.getContext().getAuthentication();
        LoginMember member=(LoginMember)authentication.getPrincipal();
        List<String> permissions = member.getPermissions();
        boolean result=false;
        for (String p : permission) {
           result|=permissions.contains(p);
        }
        return result;
    }
}
